/*
 * Copyright @ 2018 河南雪城软件有限公司  All rights reserved.
 *
 */

package com.hw.one.peach.da.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hw.one.core.controller.BaseController;
import com.hw.one.core.logger.annotation.SystemLog;
import com.hw.one.core.logger.constant.LogConstants;
import com.hw.one.core.result.MsgResult;
import com.hw.one.peach.da.mongo.IVOCsMongo;
import com.hw.one.peach.da.service.IVOCsService;

import io.swagger.annotations.ApiOperation;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.util.WebUtils;

/**
 * <p>
 * 详细描述
 * </p>
 *
 * @author Erick
 * @date 2018/4/17
 */
@RestController
@RequestMapping("vocs")
public class VOCsController extends BaseController {

	@Resource
	private IVOCsService VOCsService;

	/**
	 * 获取vocs列表
	 * @return List<map> 站点名/code 设备名/code 文件名 文件大小 文件创建时间
	 */
	@ApiOperation(value = "VOCs", notes = "获取vocs列表", httpMethod = "GET")
	@SystemLog(moduleName = "VOCs", description = "获取vocs列表", operType = LogConstants.OPER_SELECT)
	@Override
	public MsgResult get(HttpServletRequest request) {
		Map<String, Object> params = wrapParams(request);
		MsgResult result = null;
		try {
			result = VOCsService.get(params);
		} catch (Exception e) {
			log.error("无参查询异常", e);
		}
		return result;
	}
	
	/**
	 * 删除vocs数据包
	 * @params filename 文件名
	 */
	@ApiOperation(value = "VOCs", notes = "删除voc数据包", httpMethod = "DELETE")
	@SystemLog(moduleName = "VOCs", description = "删除voc数据包", operType = LogConstants.OPER_DELETE)
	@Override
	public MsgResult delete(HttpServletRequest request, @PathVariable String id) {
		MsgResult result = null;
		try {
			result = VOCsService.delete(id);
		} catch (Exception e) {
			log.error("删除异常", e);
		}
		return result;
	}
	
	/**
	 * <p>
	 * 下载vocs数据包
	 * <p>
	 * 
	 * @return MsgResult 
	 * @author zhangshaowei
	 * @create 2018年05月16日下午3:38:28
	 */
	@ApiOperation(value = "VOCs", notes = "下载voc数据包", httpMethod = "GET")
	@SystemLog(moduleName = "VOCs", description = "下载voc数据包", operType = LogConstants.OPER_SELECT)
	@RequestMapping(path = "download", method = RequestMethod.GET)
	public void download(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> params = wrapParams(request);
		try {
			String filename = String.valueOf(params.get("filename"));

			InputStream fis = VOCsService.download(filename);

			response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("UTF-8"), "ISO-8859-1"));
			response.setContentType("multipart/form-data");

			OutputStream os = response.getOutputStream();
			if (fis != null) {
				byte[] b = new byte[1024];
				int len;
				while ((len = fis.read(b)) != -1) {
					os.write(b, 0, len);
				}
				os.flush();
			}

			if (fis != null) {
				fis.close();
			}
			if (os != null) {
				os.close();
			}
		} catch (Exception e) {
			log.error("下载voc数据包异常", e);
		}
	}

	/**
	 * <p>
	 * 导入vocs定量报告
	 * <p>
	 * 
	 * @return MsgResult 
	 * @author zhangshaowei
	 * @create 2018年05月16日下午3:38:28
	 */
	@ApiOperation(value = "VOCs", notes = "导入vocs定量报告", httpMethod = "POST")
	@SystemLog(moduleName = "VOCs", description = "导入vocs定量报告", operType = LogConstants.OPER_ADD)
	@RequestMapping(path = "upload", method = RequestMethod.POST)
	public MsgResult upload(HttpServletRequest request) {
		Map<String, Object> params = wrapParams(request);
		try {
			MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class);
			Map<String, MultipartFile> files = multipartRequest.getFileMap();

			VOCsService.upload(params, files);

			return MsgResult.okResult();
		} catch (Exception e) {
			log.error("导入vocs定量报告异常", e);
			return MsgResult.exceptionResult();
		}
	}
	
	/**
	 * <p>
	 * 导入vocs定量报告
	 * <p>
	 * 
	 * @return MsgResult 
	 * @author zhangshaowei
	 * @create 2018年05月16日下午3:38:28
	 */
	@ApiOperation(value = "VOCs", notes = "导入vocs定量报告", httpMethod = "POST")
	@SystemLog(moduleName = "VOCs", description = "导入vocs定量报告", operType = LogConstants.OPER_ADD)
	@RequestMapping(path = "import", method = RequestMethod.POST)
	public MsgResult importExcel(HttpServletRequest request) {
		Map<String, Object> params = wrapParams(request);
		try {
			MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class);
			Map<String, MultipartFile> files = multipartRequest.getFileMap();

			VOCsService.importExcel(params, files);

			return MsgResult.okResult();
		} catch (Exception e) {
			log.error("导入vocs定量报告异常", e);
			return MsgResult.exceptionResult();
		}
	}
	
	/**
	 * <p>
	 * 获取voc树菜单
	 * <p>
	 * 
	 * @return JSONArray 
	 * @author zhangshaowei
	 * @create 2018年05月16日下午3:38:28
	 */
	@ApiOperation(value = "VOCs", notes = "获取voc树菜单", httpMethod = "GET")
	@SystemLog(moduleName = "VOCs", description = "获取voc树菜单", operType = LogConstants.OPER_SELECT)
	@RequestMapping(path = "tree", method = RequestMethod.GET)
	public MsgResult vocTree(HttpServletRequest request) {
		return MsgResult.okResult(VOCsService.vocTree());
	}
	
	/**
	 * <p>
	 * 获取所有组分JSON数组
	 * <p>
	 * 
	 * @return JSONArray 
	 * @author zhangshaowei
	 * @create 2018年05月16日下午3:38:28
	 */
	@ApiOperation(value = "VOCs", notes = "获取所有组分JSON数组", httpMethod = "GET")
	@SystemLog(moduleName = "VOCs", description = "获取所有组分JSON数组", operType = LogConstants.OPER_SELECT)
	@RequestMapping(path = "component", method = RequestMethod.GET)
	public MsgResult vocComponent(HttpServletRequest request) {
		return MsgResult.okResult(VOCsService.componentJson());
	}
}
